<?php

/***
 * Class CW_SessionClass provides a layer of abstraction for dealing with PHP sessions:
 ***/
class CW_SessionClass {

   /**
	* All cookies that have been set at instantiation
	* @access Private
	* @var Array
	*/
	private $cookies;


   /**
	* The cookie path
	* @access Private
	* @var String
	*/
	private $path;


   /**
	* The domain in which this cookie will reside
	* @access Private
	* @var String
	*/
	private $domain;


   /**
	* The prefix for cookie names. This allows you to create a separate
	* namespace for cookies used within different applications / areas
	* @access Private
	* @var String
	*/
	private $prefix;


   // ! Constructor Method

   /**
	* Instantiates class and defines instance variables.
	*
	* @param Array $cookies Cookie array passed by reference
	* @param String $path The cookie path
	* @param String $domain The cookie domain
	* @param String $prefix The cookie prefix
	* @author Daniel Wilhelm II Murdoch <wilhelm.murdoch@gmail.com>
	* @since v 1.0.0
	* @access public
	* @return Void
	*/
	public function __construct(&$cookies, $path = '/', $domain = '', $prefix = '')
	{
		$this->cookies = &$cookies;
		$this->path = $path;
		$this->domain = $domain;
		$this->prefix = $prefix;
	}


   // ! Accessor Method

   /**
	* Returns a specified cookie value.
	*
	* @param String $key Name of the cookie to fetch
	* @param Mixed $default The default value to return if one cannot be found
	* @author Daniel Wilhelm II Murdoch <wilhelm.murdoch@gmail.com>
	* @since v 1.0.0
	* @access Public
	* @return Mixed
	*/
	public function getCookie($key, $default = false)
	{
		$key = $this->getKey($key);

		return isset($this->cookies[$key]) ? $this->cookies[$key] : $default;
	}


   // ! Executor Method

   /**
	* Bakes a new cookie... YUM!
	*
	* @param String $key Name of the cookie to set
	* @param String $value The value of the new cookie
	* @param Integer $expires The time, in seconds, when this cookie expires
	* @author Daniel Wilhelm II Murdoch <wilhelm.murdoch@gmail.com>
	* @since v 1.0.0
	* @access Public
	* @return True
	*/
	public function setCookie($key, $value, $expires = 0)
	{
		$key = $this->getKey($key);

		@setcookie($key, $value, ($expires ? time() + $expires : $expires), $this->path, $this->domain);

		$this->cookies[$key] = $value;

		return true;
	}


   // ! Executor Method

   /**
	* Removes a cookie from our list.
	*
	* @param String $key The name of the cookie to remove
	* @author Daniel Wilhelm II Murdoch <wilhelm.murdoch@gmail.com>
	* @since v 1.0.0
	* @access Public
	* @return Boolean
	*/
	public function unsetCookie($key)
	{
		$key = $this->getKey($key);

		if(isset($this->cookies[$key]))
		{
			unset($this->cookies[$key]);
		}

		return @setcookie($key, '', -1, $this->path, $this->domain);;
	}


   // ! Executor Method

   /**
	* A convenience method that returns a specified key with the
	* current cookie prefix.
	*
	* @param String $key The name of the cookie to prefix
	* @author Daniel Wilhelm II Murdoch <wilhelm.murdoch@gmail.com>
	* @since v 1.0.0
	* @access Private
	* @return String
	*/
	private function getKey($key)
	{
		return $this->prefix . $key;
	}
}

?>